﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TA.Win.Utils.Controls
{
	public partial class PathUC : UserControl
	{
		#region PUBLIC
		#region INIT
		public PathUC()
		{
			InitializeComponent();

			this.Mode = PathBrowsingMode.OpenFile;
		}
		#endregion
		#region EVENTS
		#endregion
		#region PROPS
		[DefaultValue(PathBrowsingMode.OpenFile)]
		public PathBrowsingMode Mode { get; set; }
		[DefaultValue("")]
		public string Value
		{
			get
			{
				return this.txt.Text;
			}
			set
			{
				this.txt.Text = "";
			}
		}
		#endregion

		#region METHODS
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		FolderBrowserDialog _fbd;
		OpenFileDialog _ofd;
		SaveFileDialog _sfd;
		#endregion
		#region PROPS
		FolderBrowserDialog FBD
		{
			get
			{
				if (this._fbd == null)
				{
					this._fbd = new FolderBrowserDialog();
				}
				return this._fbd;
			}
		}
		OpenFileDialog OFD
		{
			get
			{
				if (this._ofd == null)
				{
					this._ofd = new OpenFileDialog();
				}
				return this._ofd;
			}
		}
		SaveFileDialog SFD
		{
			get
			{
				if (this._sfd == null)
				{
					this._sfd = new SaveFileDialog();
				}
				return this._sfd;
			}
		}
		#endregion
		#region METHODS
		#region INIT
		#endregion
		#region DATA
		#endregion
		#region HANDLERS
		private void btn_Click(object sender, EventArgs e)
		{
			CommonDialog dlg;
			switch (this.Mode)
			{
				case PathBrowsingMode.OpenFile:
					dlg = this.OFD; break;
				case PathBrowsingMode.SaveFile:
					dlg = this.SFD; break;
				case PathBrowsingMode.Folder:
					dlg = this.FBD; break;
				default:
					throw new Exception();
			}

			DialogResult dr = dlg.ShowDialog();
			if (dr == DialogResult.OK)
			{
				if (dlg is FileDialog)
				{
					this.txt.Text = ((FileDialog)dlg).FileName;
				}
				else if (dlg is FolderBrowserDialog)
				{
					this.txt.Text = ((FolderBrowserDialog)dlg).SelectedPath;
				}
			}
		}
		#endregion
		#endregion
		#endregion				
				
	}
}
